package com.lineage.data.item_etcitem.wand;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1MonsterInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Location;
import com.lineage.server.model.L1Object;
import com.lineage.server.serverpackets.S_ChangeHeading;
import com.lineage.server.serverpackets.S_DoActionGFX;
import com.lineage.server.serverpackets.S_EffectLocation;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import java.util.ArrayList;
import java.util.Iterator;

public class FireWand extends ItemExecutor {
  public static ItemExecutor get() {
    return new FireWand();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    if (pc.getMapId() != 2101) {
      L1ItemInstance[] itemlist = pc.getInventory().findItemsId(92122);
      int i = 0;
      while (i < itemlist.length) {
        pc.getInventory().removeItem(itemlist[i]);
        i++;
      } 
      itemlist = pc.getInventory().findItemsId(92123);
      i = 0;
      while (i < itemlist.length) {
        pc.getInventory().removeItem(itemlist[i]);
        i++;
      } 
      return;
    } 
    int spellsc_objid = data[0];
    int spellsc_x = data[1];
    int spellsc_y = data[2];
    L1Object target = World.get().findObject(spellsc_objid);
    if (item.getChargeCount() == 0) {
      pc.getInventory().deleteItem(item);
      return;
    } 
    if (target != null) {
      doWandAction(pc, target);
    } else {
      pc.sendPacketsXR((ServerBasePacket)new S_EffectLocation(new L1Location(spellsc_x, spellsc_y, pc.getMapId()), 762), 7);
    } 
    pc.sendPacketsX8((ServerBasePacket)new S_DoActionGFX(pc.getId(), 17));
    int useCount = item.getChargeCount() - 1;
    if (useCount > 0) {
      item.setChargeCount(useCount);
      pc.getInventory().updateItem(item, 128);
    } else {
      pc.getInventory().deleteItem(item);
    } 
  }
  
  private void doWandAction(L1PcInstance user, L1Object target) {
    if (user.getId() == target.getId())
      return; 
    if (!user.glanceCheck(target.getX(), target.getY()))
      return; 
    ArrayList<L1Object> list = World.get().getVisibleObjects(target, 3);
    if (list == null)
      return; 
    Iterator<L1Object> iterator = list.iterator();
    while (iterator.hasNext()) {
      L1Object object = iterator.next();
      if (object instanceof L1MonsterInstance) {
        L1MonsterInstance dota = (L1MonsterInstance)object;
        dota.broadcastPacketAll((ServerBasePacket)new S_DoActionGFX(dota.getId(), 2));
        dota.receiveDamage((L1Character)user, 400);
      } 
    } 
    if (target instanceof L1MonsterInstance) {
      L1MonsterInstance mob = (L1MonsterInstance)target;
      mob.broadcastPacketAll((ServerBasePacket)new S_DoActionGFX(target.getId(), 2));
      mob.receiveDamage((L1Character)user, 400);
      user.sendPacketsXR((ServerBasePacket)new S_EffectLocation(new L1Location(mob.getX(), mob.getY(), user.getMapId()), 762), 7);
    } 
    user.setHeading(user.targetDirection(target.getX(), target.getY()));
    user.sendPacketsX10((ServerBasePacket)new S_ChangeHeading((L1Character)user));
  }
}
